package weaver.jobs.zhlx.duss.cron;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.jobs.zhlx.duss.utils.ZhlxJobUtil;

/**
 * @Title: ecology
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-12-06
 * @Description: 定时调用SAP客户主数据接口同步客户主数据到建模表单
 * @ModifyLog:
 **/

public class WeaSyncKhBySapCron extends BaseCronJob {

    @Override
    public void execute() {
        BaseBean baseBean = new BaseBean();
        try {
            ZhlxJobUtil jobUtil = new ZhlxJobUtil();
            String tablename = "uf_sapkhzsj";
            int formmodeid = jobUtil.getFormmodeid(tablename);
            JSONObject dataJson = new JSONObject();
            dataJson.put("BUKRS", "");
            dataJson.put("KUNNR", "");
            JSONObject resultJson = jobUtil.handleOKHttpRequest("FI035", dataJson);
            String code = Util.null2String(resultJson.getString("code"));
            baseBean.writeLog(">>>>>>WeaSyncKhBySapCron>>>>code=" + code);
            if ("success".equals(code)) {
                String output = Util.null2String(resultJson.getString("OUTPUT"));
                JSONObject outputJson = JSONObject.parseObject(output);
                String msgType = outputJson.getString("MSG_TYPE");
                String msgText = outputJson.getString("MSG_TEXT");
                baseBean.writeLog(">>>>>>WeaSyncKhBySapCron>>>>msgType=" + msgType + ",msgText=" + msgText);
                if ("S".equals(msgType) && "调用成功".equals(msgText)) {
                    String dataReturnStr = outputJson.getString("DATA");
                    JSONObject dataReturnJson = JSONObject.parseObject(dataReturnStr);
                    String returnData = dataReturnJson.getString("RETURN");
                    JSONArray returnList = JSONArray.parseArray(returnData);
                    for (int i = 0; i < returnList.size(); i++) {
                        JSONObject paramJson = new JSONObject();
                        JSONObject returnJson = returnList.getJSONObject(i);
                        String returnHeaderStr = returnJson.getString("HEADER");
                        JSONObject returnHeaderJson = JSONObject.parseObject(returnHeaderStr);
                        paramJson.put("khzhz", returnHeaderJson.getString("KTOKD"));
                        String khbh = returnHeaderJson.getString("KUNNR");
                        paramJson.put("khbh", khbh);
                        String mc1 = returnHeaderJson.getString("NAME1");
                        String mc2 = returnHeaderJson.getString("NAME2");
                        paramJson.put("mc1", mc1);
                        paramJson.put("mc2", mc2);
                        paramJson.put("khmcq", mc1 + mc2);
                        paramJson.put("sstj1", returnHeaderJson.getString("BU_SORT1"));
                        paramJson.put("gj", returnHeaderJson.getString("LAND1"));
                        paramJson.put("dq", returnHeaderJson.getString("REGIO"));
                        paramJson.put("cs", returnHeaderJson.getString("ORT01"));
                        paramJson.put("yzbm", returnHeaderJson.getString("PSTLZ"));
                        paramJson.put("jd", returnHeaderJson.getString("STRAS"));
                        paramJson.put("yydm", returnHeaderJson.getString("SPRAS"));
                        paramJson.put("dh", returnHeaderJson.getString("TELF1"));
                        paramJson.put("yddh", returnHeaderJson.getString("TELF2"));
                        paramJson.put("cz", returnHeaderJson.getString("TELFX"));
                        paramJson.put("email", returnHeaderJson.getString("SMTP_ADDR"));
                        paramJson.put("wysbh", returnHeaderJson.getString("IDNUMBER"));
                        paramJson.put("bslx", returnHeaderJson.getString("TYPE"));
                        paramJson.put("jzdj", returnHeaderJson.getString("XBLCK"));
                        paramJson.put("myhb", returnHeaderJson.getString("VBUND"));
                        paramJson.put("gzdjsygs", returnHeaderJson.getString("SPERR"));
                        int mainid = jobUtil.getMainId(tablename, "khbh", khbh);
                        if (mainid <= 0) {
                            int dataid = jobUtil.insertModelData(formmodeid, paramJson);
                            String bankStr = returnHeaderJson.getString("BANK");
                            JSONArray bankList = JSONArray.parseArray(bankStr);
                            if (!bankList.isEmpty()) {
                                for (int k = 0; k < bankList.size(); k++) {
                                    JSONObject paramsJson = new JSONObject();
                                    paramsJson.put("yxgjdm", bankList.getJSONObject(k).getString("BANKS"));
                                    paramsJson.put("yxdm", bankList.getJSONObject(k).getString("BANKL"));
                                    paramsJson.put("yxzh", bankList.getJSONObject(k).getString("BANKN"));
                                    paramsJson.put("ckmx", bankList.getJSONObject(k).getString("BKREF"));
                                    paramsJson.put("swift", bankList.getJSONObject(k).getString("SWIFT"));
                                    paramsJson.put("kzm", bankList.getJSONObject(k).getString("BKONT"));
                                    paramsJson.put("gjyxzh", bankList.getJSONObject(k).getString("IBAN"));
                                    paramsJson.put("zhcyr", bankList.getJSONObject(k).getString("KOINH"));
                                    paramsJson.put("mainid", dataid);
                                    jobUtil.insertDetailData(tablename + "_dt1", paramsJson);
                                }
                            }
                            String bukrsStr = returnHeaderJson.getString("BUKRS");
                            JSONArray bukrsList = JSONArray.parseArray(bukrsStr);
                            if (!bukrsList.isEmpty()) {
                                for (int j = 0; j < bukrsList.size(); j++) {
                                    JSONObject paramsJson = new JSONObject();
                                    paramsJson.put("gsdm", bukrsList.getJSONObject(j).getString("BUKRS"));
                                    paramsJson.put("tykm", bukrsList.getJSONObject(j).getString("AKONT"));
                                    paramsJson.put("zb", bukrsList.getJSONObject(j).getString("KNRZE"));
                                    paramsJson.put("pxm", bukrsList.getJSONObject(j).getString("ZUAWA"));
                                    paramsJson.put("xqdbh", bukrsList.getJSONObject(j).getString("ALTKN"));
                                    paramsJson.put("ygh", bukrsList.getJSONObject(j).getString("PERNR"));
                                    paramsJson.put("fktj", bukrsList.getJSONObject(j).getString("ZTERM"));
                                    paramsJson.put("rcz", bukrsList.getJSONObject(j).getString("TOGRU"));
                                    paramsJson.put("fkfs", bukrsList.getJSONObject(j).getString("ZWELS"));
                                    paramsJson.put("khyx", bukrsList.getJSONObject(j).getString("HBKID"));
                                    paramsJson.put("gzdjsxgs", bukrsList.getJSONObject(j).getString("SPERR1"));
                                    paramsJson.put("mainid", dataid);
                                    jobUtil.insertDetailData(tablename + "_dt2", paramsJson);
                                }
                            }
                        } else {
                            jobUtil.updateModelData(tablename, mainid, paramJson);
                            String bankStr = returnHeaderJson.getString("BANK");
                            baseBean.writeLog(">>>>>>WeaSyncKhBySapCron>>>>bankStr=" + bankStr);
                            JSONArray bankList = JSONArray.parseArray(bankStr);
                            if (!bankList.isEmpty()) {
                                jobUtil.deleteDetailData(tablename + "_dt1", mainid);
                                for (int k = 0; k < bankList.size(); k++) {
                                    JSONObject paramsJson = new JSONObject();
                                    paramsJson.put("yxgjdm", bankList.getJSONObject(k).getString("BANKS"));
                                    paramsJson.put("yxdm", bankList.getJSONObject(k).getString("BANKL"));
                                    paramsJson.put("yxzh", bankList.getJSONObject(k).getString("BANKN"));
                                    paramsJson.put("ckmx", bankList.getJSONObject(k).getString("BKREF"));
                                    paramsJson.put("swift", bankList.getJSONObject(k).getString("SWIFT"));
                                    paramsJson.put("kzm", bankList.getJSONObject(k).getString("BKONT"));
                                    paramsJson.put("gjyxzh", bankList.getJSONObject(k).getString("IBAN"));
                                    paramsJson.put("zhcyr", bankList.getJSONObject(k).getString("KOINH"));
                                    paramsJson.put("mainid", mainid);
                                    jobUtil.insertDetailData(tablename + "_dt1", paramsJson);
                                }
                            }
                            String bukrsStr = returnHeaderJson.getString("BUKRS");
                            baseBean.writeLog(">>>>>>WeaSyncKhBySapCron>>>>bankStr=" + bankStr);
                            JSONArray bukrsList = JSONArray.parseArray(bukrsStr);
                            if (!bukrsList.isEmpty()) {
                                jobUtil.deleteDetailData(tablename + "_dt2", mainid);
                                for (int j = 0; j < bukrsList.size(); j++) {
                                    JSONObject paramsJson = new JSONObject();
                                    paramsJson.put("gsdm", bukrsList.getJSONObject(j).getString("BUKRS"));
                                    paramsJson.put("tykm", bukrsList.getJSONObject(j).getString("AKONT"));
                                    paramsJson.put("zb", bukrsList.getJSONObject(j).getString("KNRZE"));
                                    paramsJson.put("pxm", bukrsList.getJSONObject(j).getString("ZUAWA"));
                                    paramsJson.put("xqdbh", bukrsList.getJSONObject(j).getString("ALTKN"));
                                    paramsJson.put("ygh", bukrsList.getJSONObject(j).getString("PERNR"));
                                    paramsJson.put("fktj", bukrsList.getJSONObject(j).getString("ZTERM"));
                                    paramsJson.put("rcz", bukrsList.getJSONObject(j).getString("TOGRU"));
                                    paramsJson.put("fkfs", bukrsList.getJSONObject(j).getString("ZWELS"));
                                    paramsJson.put("khyx", bukrsList.getJSONObject(j).getString("HBKID"));
                                    paramsJson.put("gzdjsxgs", bukrsList.getJSONObject(j).getString("SPERR1"));
                                    paramsJson.put("mainid", mainid);
                                    jobUtil.insertDetailData(tablename + "_dt2", paramsJson);
                                }
                            }
                        }


                    }
                }
            }
        } catch (Exception e) {
            baseBean.writeLog(">>>>>>WeaSyncKhBySapCron>>>>Exception=" + e);
        }

    }


}

